#include "Menu.h"
#include <iostream>
//additional includes


using namespace std;

Menu::Menu()
{
}

void Menu::display_main_menu() const 
{

}
void Menu::display_info() const
{
		//Line below is commented, replaced by lines below it
	//cout << "Here is the Info requested \n";//display Project Part I, team name and team members
	
	//Line I added to display team name and members
	cout << "Project Part I\n";
	cout << "Maroon 567-17 Team\n"; //forget team name :p
	cout << "- Nick Giunta\n";
	cout << "- Vincent Nguyen\n";
	cout << "- Rendy Bambang\n";
}

//NJG part 3S
vector<Category> list1(string x){ //Category vector

	ifstream data;    //filestream named data
	data.open(x.c_str());    //open filestream of input file any file type works
	int number;  
	int cat;
	string name;
	Category c;
	vector<Category>v;  //vector to store the categories
	data >> number;  //filestream should put the first number which denotes the number of following lines of a type into number.
	for(int i=0; i<number;++i){
		data >> cat >> name; //read from data and store in int and string
		c= Category(cat, name); //construct from int and string
		v.push_back(c);  //construct category type from data and store in vector
	}
	if(data.fail()) cout << "Error file does not exist\n";
	else cout << "Categories read successfully\n";
	data.close();
	return v;
}

vector<Product> list2(string x){ //Product vector
	ifstream data;
	data.open(x.c_str());  
	int number;
	int cat;
	string name;
	double price;
	int id;
	Product p;
	vector<Product>v;
	data>> number;
	for (int i=0; i<number;++i) data >> cat >> name; //Get rid of the values already read by the category function
	data>> number;
	for(int i=0; i<number;++i){
		data >> id >> cat >> name >> price;
		p = Product(id, cat, name, price);
		v.push_back(p);
	}
	if(data.fail()) cout << "Error reading product data\n";
	else cout << "Products read successfully\n";
	data.close();
	return v;
}

void Menu::read()
{
	int x;
	cout << "Choose an option to read:\n\t1) Customer data"<<endl;
	cout << "\t2) Cart and cart item data\n\t3) Category and product data\n> ";
	cin >> x;
	switch(x){
		case 3:
			{/*Nick's read
			cout << "Read file name then read store data \n";
			*/
			string file;        //Filename
			cout << "Enter file name or file path to read from (include file's type)\n> ";
			cin >> file;
			cout << "Reading data...\n";
			cats=list1(file);
			prods=list2(file);
			
			}
			break;
		case 2:
			{
			
			/*Rendy's read
			cout << "Read file name then read store data \n";*/
			ifstream fin;
			//open file stream
			fin.open("../../Sample_Data/carts.dat");
			int n;
			//read number of cart
			fin >> n;
			//read all cart
			if(fin.is_open()){
			for(int i = 0; i < n; i++){
				Cart cart;
				int cart_id, cust_id, y, m, d;
				//read line
				fin >> cart_id >> cust_id >> y >> m >> d;
				//set vars
				cart = Cart(cart_id, cust_id, Date(y, (Date::Month)m, d));
				//push to vector
				carts.push_back(cart);
			}
			fin >> n;
			for(int i = 0; i < n; i++){
				Cart_item cart_item;
				int cart_item_id, cart_id, prod_id, prod_quantity;
				//read line
				fin >> cart_item_id >> cart_id >> prod_id >> prod_quantity;
				//set vars
				cart_item = Cart_item(cart_item_id, cart_id, prod_id, prod_quantity);
				//push to vector
				cart_items.push_back(cart_item);
			}
			cout<<"Cart data read successfully"<<endl;
			cout<<"Cart item data read successfully"<<endl;
			fin.close();
			}
			else cout<<"Error reading data"<<endl;
			}
			break;

		case 1:
			{
			/*cout << "Read file name then read store data \n";
			Below read() modifications made by VCN for part A1*/
			ifstream cust_data ("../../Sample_Data/cust.dat");
			int cust_id, house_num, zip_code, n;	//necessary info
			string cust_name, street, line;
			Customer cust;							//temp customer
			if(cust_data.is_open()){
			
			cust_data >> n;
			for(int i = 0; i < n; i++){
				
				//read line
				cust_data >> cust_id >> cust_name >> house_num >> street >> zip_code;
				//set vars
				cust = Customer(cust_id, cust_name, Street_address(house_num,street,zip_code));
				//push to vector
				custs.push_back(cust);
			}
						
				cout << "Customer data read successfully\n";
				cust_data.close();
			}
			else cout<<"Error reading data\n";
			}
			break;
		default:
			cout << "Invalid Input"<<endl;
			break;
	}
}

//{ compare functions
bool CatCompare(Category a, Category b){
        return a.getCatId() < b.getCatId();
}

bool CustCompare(Customer a, Customer b){
        return a.getCustName() < b.getCustName();
}

bool ProdCompare(Product a, Product b){
        return a.getProdPrice() < b.getProdPrice();
}
bool QtyCompare(Cart_item lhs, Cart_item rhs){
		return lhs.getProdQty()>rhs.getProdQty();
	}
//}


void Menu::show() const 
{
	int prompt;
	cout << "Choose an option to show:\n\t1) Categories"<<endl;
	cout << "\t2) Customers\n\t3) Products\n\t4) Product details in a Cart\n> ";
	cin >> prompt;
	switch(prompt){
		
		case 1:{
			//NJG 1S
			
			vector<Category>sorted; //vector to recieve sorted data
			sorted=cats;
			
			sort(sorted.begin(),sorted.end(),CatCompare); 
			
			cout << "Category:\tProducts in Category:\n";
			for(int i=0; i<sorted.size();++i){
				int products(0);
				for(int j=0; j<prods.size();++j){
					if(sorted[i].getCatId()==prods[j].getCatId()) ++products; //increment products the category id matches a products category id and then cout the data.
				}
				cout << left<<setw(16)<< sorted[i].getCatName() << products <<'\n';
			}
			break;}
			//end NJG 1S
		case 2:{
			//VCN show function PartB S2 
			cout<<"Customer ID"<<'\t'<<"Customer Name"<<'\t'<<"House Number"<<'\t'<<"Street Name"<<"\t"<<"Zip Code\n";
			vector<Customer> tempCust; //temporary Customer vector to sort
			tempCust=custs;
			sort(tempCust.begin(),tempCust.end(), CustCompare); //uses above sort function for alphabetic sort
			for (int i=0; i<tempCust.size(); i++)
			{
				cout << tempCust[i].display();
			}
			break;}
		case 3:{
		vector<Product> tempProd;
        for(int i=0; i<prods.size(); i++){
                tempProd.push_back(prods[i]);
        }
        sort(tempProd.begin(), tempProd.end(), ProdCompare); //sort based on price
        cout<<"prod_id\tcat_id\tprod_name\tprod_price\n";
        for( int i=0; i<tempProd.size(); i++){                
                cout << tempProd[i].display() << "\n";
        }
			break;}
		case 4:{
			//VCN PartC2 a)
			bool check=false;
			string prod_name;
			int prod_price, prod_qty, prod_id, cart_id;
			vector<Cart_item>temp_items;
			cout<<"Enter a cart ID to look at: ";
			cin>> cart_id;
			
			for (int i=0; i<carts.size(); i++){
				if (cart_id==carts[i].getCartId()) check=true; //check if cart ID exists
			}
			if (!check){ cout<<"Cart ID doesn't exist"; return; } //return otherwise
			cout<<"Here are the details for cart ID "<< cart_id<<endl;
			cout<<"Cart Item ID\tProduct Name\tProduct Price\tQuantity in Cart"<<endl;
			
			for (int j=0; j<cart_items.size(); j++){
				if(cart_items[j].getCartId()==cart_id){
					temp_items.push_back(cart_items[j]);
			}}
			sort(temp_items.begin(),temp_items.end(),QtyCompare);
			for (int k=0; k<temp_items.size(); k++){
				for (int m=0; m<prods.size(); m++){
					if (prods[m].getProdId()==temp_items[k].getProdId()){
						cout<<left<<setw(16)<<temp_items[k].getCartItemId();
						cout<<left<<setw(16)<<prods[m].getProdName();
						cout<<left<<setw(16)<<prods[m].getProdPrice();
						cout<<temp_items[k].getProdQty()<<endl;
					}
				}	
			}
			
			break;}		
			
		default: cout<<"Invalid Input"; break;
		
		}}

void Menu::find() const 
{
	int prompt;
	cout << "Choose an option to find:\n\t1) Total sales for a product"<<endl;
	cout << "\t2) Total sales for a customer\n\t3) Total sales for a category\n> ";
	cin >> prompt;
	switch(prompt){
		case 1:{
			//cout << "Find queries \n";
			cout<<"Enter 1 to search by Product name or 2 for Product ID\n> ";
			int i;
			cin>>i;
			double sum=0;
			string prod_name=" ";
			int cart_id;
			double prod_qty;
			double prod_price; 
			int prod_id=-1;
			
			switch(i){
			
				case 1:{
				//VCN PartB S1
					cout<<"Input the product name to search for: ";
					cin>>prod_name;
					for (int i=0 ;i<prods.size();i++){ 
						if(prods[i].getProdName()==prod_name)
						{ prod_id=prods[i].getProdId();
						  prod_price=prods[i].getProdPrice();}}
					if (prod_id==-1) {cerr<<"The Product name was not found"<<endl; return;}
			
					break;
					}
				case 2:{
					cout<<"Input the ID number to search for: ";
					cin>>prod_id;
				
					for (int i=0 ;i<prods.size();i++){ 
						if(prods[i].getProdId()==prod_id) 
						{ prod_name=prods[i].getProdName();
						  prod_price=prods[i].getProdPrice();}}
					if (prod_name==" "){ cerr<<"The Product ID was not found"<<endl; return;}
					break;}
				
				default:
					cout<<"Invalid Input\n"; return; break;
				}

						
				for(int k=0; k<cart_items.size(); k++)
				{
					if (cart_items[k].getProdId()==prod_id) //looks for prod id in the cart_item vector
					{
						prod_qty=cart_items[k].getProdQty(); //found the Prod qty for cart id
						sum+=prod_price*prod_qty; //add to sum
					}}
			cout.precision(2);
			cout.setf(ios::fixed,ios::floatfield);
			cout<<"The total sales for "<<prod_name<<" is $"<<sum<<endl;
		
			break;}
		case 2:{
			//cout << "Find queries \n";
			//VCN find function PartB S2
			cout<<"Enter 1 to search by Customer name or 2 for Customer ID\n> ";
			int i;
			cin>>i;
			double sum=0;
			string cust_name=" ";
			int cart_id, prod_id;
			double prod_qty;
			double prod_price; 
			int cust_id=-1;
			
			switch(i){
			
				case 1:{
					cout<<"Input the customer name to search for: ";
					cin>>cust_name;
					for (int i=0 ;i<custs.size();i++){ 
						if(custs[i].getCustName()==cust_name) {cust_id=custs[i].getCustId();}}
					if (cust_id==-1) {cerr<<"The Customer name was not found"<<endl; return;}
			
					break;
					}
				case 2:{
					cout<<"Input the ID number to search for: ";
					cin>>cust_id;
					//get the name of the customer
					for (int i=0 ;i<custs.size();i++){ 
						if(custs[i].getCustId()==cust_id) {cust_name=custs[i].getCustName();}}
					if (cust_name==" "){ cerr<<"The Customer ID was not found"<<endl; return;}
					break;}
				
				default:
					cout<<"Invalid Input\n"; return; break;
				}
				for (int j=0; j<carts.size();j++)
				{
					if (carts[j].getCustId()==cust_id) //looks for customer id in carts vector
					{
						cart_id=carts[j].getCartId(); //found the cart id for a customer
				
						for(int k=0; k<cart_items.size(); k++)
						{
							if (cart_items[k].getCartId()==cart_id) //looks for cart id in the cart_item vector
							{
								prod_id=cart_items[k].getProdId(); //found Prod id for the cart id
								prod_qty=cart_items[k].getProdQty(); //found the Prod qty for cart id
							
								for (int m=0; m<prods.size(); m++)
								{
									if (prods[m].getProdId()==prod_id) //looks for product in prods vector
									{
										prod_price=prods[m].getProdPrice(); //found the product price
										sum+=prod_price*prod_qty;
										
									}}}}}}
			cout.precision(2);
			cout.setf(ios::fixed,ios::floatfield);
			cout<<"The total sales for "<<cust_name<<" is $"<<sum<<endl;
			break;}
		case 3:{
			//Rendy PartB S3
			cout << "Insert category id :";
			int cat_id;
			cin >> cat_id;
			vector<int> prodIds;
			vector<double> prodPrices;
			double total_sale = 0;
			
			//looking for product ids for certain category
			for( int i=0; i<prods.size(); i++){
				if(prods[i].getCatId()==cat_id){
					prodIds.push_back(prods[i].getProdId());
					prodPrices.push_back(prods[i].getProdPrice());
				}
			}
			//calculating total sale by looking for product and quantity
			for( int i=0; i<prodIds.size(); i++){
				for( int j=0; j<cart_items.size(); j++ ){
					if(cart_items[j].getProdId()==prodIds.at(i)){				
						total_sale += prodPrices.at(i)*cart_items[i].getProdQty();
					}		
				}
			}
				
			cout << "Total sale : " << total_sale << "\n";
		
			break;}
		default: cout<<"Invalid Input"; break;
	
}}

void Menu::update()  
{
	int prompt;
	cout << "Choose a field to update:\n\t1) Products and Categories"<<endl;
	cout << "\t2) Customers\n\t3) Carts and Cart Items\n> "; 
	
	cin >> prompt;
	switch(prompt){
		case 1:{
			cout << "What would you like to update:\n\t1) Add a new product\n\t2) Delete a product from a category\n\t3) Change a product price\n\t4) Add a new category\n> ";
			int x;
			cin >> x;
			switch(x){
				case 1:{
					cout << "Updating Products\n";
					int b;
					cout << "How many additional entries do you want to add?\n> "; //Find how many additional enteries user wants.
					cin >> b;
			
					int tracker = prods.size();
					Product p;
					Category c;
					int prod_id, cat_id;
					double prod_price;
					string name;
					for(int i=0;i<b;++i){
						cout << "Enter product ID, category ID, product name, and product price.\n> ";
						cin >> prod_id >> cat_id >> name >> prod_price;  //enter updated info
						for(int j=0;j<cats.size();++j){ 
							if (cat_id == cats[j].getCatId()){ //check that category exists. If it does add the data to product vector.
								p=Product(prod_id, cat_id, name, prod_price);
								prods.push_back(p); 
						
							}
						}
					}
			
					if(prods.size()==tracker && b==1) cout << "Error category does not exist. No data updated:\n"; //checking success of updating.
					else if(prods.size()==tracker && b>1) cout << "Error categories do not exist. No data updated:\n";
					else if(prods.size()>tracker && prods.size()<tracker+b){
						cout << "Some categories enter entered do not exist, however the data was still updated\n";
						cout << "The products are now as follows:\n";
						p.printup(prods);
					}
					else if(prods.size()==tracker+b){
						cout << "The products are now as follows:\n"; 
						p.printup(prods); //print the whole product vector with new data at end
						}
					else cout << "Error occured";
					break;
					   }
				//NJG Part C
				case 2:{
					string x,a;
					int y;
					vector<Product>v=prods;
					vector<Category>vec=cats;
					cout << "Enter the category to delete product from:\n>";
					cin >> x;
					;
					for(int i=0;i<vec.size();++i){
						if(vec[i].getCatName()==x) y=vec[i].getCatId(); //Find the ID of named category
					}
					if (y==0){
						cout << "Error category does not exist.\n";
						break;
					}
					cout << "Current products in category are:\n";
					for(int i=0;i<v.size();++i){
						if(v[i].getCatId()== y) cout << v[i].getProdName() << '\n'; //Output all products with a cat ID matching the one found from the given name.
					}
					cout << "Enter product name to delete\n>";
					cin >> a;
					int z=v.size();
					for (int i=0;i<v.size();++i){
						if(v[i].getProdName()==a){ 
							v.erase(v.begin()+i);  //erase the entry with a name matching the one input
						}
					}
					if(v.size()==prods.size()) cout << "Error " << a << " is not a valid product name\n"; //Only true if nothing was deleted.
					else cout << "Product deleted\n";{
					cout << "Final products in category are:\n";  //Output data after deletion
					for(int i=0;i<v.size();++i){
						if(v[i].getCatId()== y) cout << v[i].getProdName() << '\n';
					}
					}
					break;
					}
				case 3:{
					string x;
					Product p;
					double y;
					vector<Product>v=prods;
					cout << "Enter name of product you want to change the price of:\n>";
					cin >> x;
					int tracker(0);
					for(int i=0;i<v.size();++i){
						tracker=i; //keeps track of the place in the vector that wanted product is so its changed price can be checked against initial price.
						if (v[i].getProdName()==x){
							cout << "The current price of " << x << " is $" << v[i].getProdPrice() << "\nEnter new price:\n>";
							cin >> y;
							p = Product(v[i].getProdId(),v[i].getCatId(),v[i].getProdName(),y); //make a new product with all the same data as the previous one except for the price which is now the one input
							v[i]=p; //assign the new product to the spot the previous one occupied
							cout << "The new price for " << v[i].getProdName() << " is $" << v[i].getProdPrice() << '\n';
							break;
						}
					}
					if (prods[tracker].getProdPrice() == v[tracker].getProdPrice()) cout << "Error changing price.\n";
					break;
					   }
				case 4:{
					string x;
					int y,z;
					Category c;
					vector<Category>v=cats;
					cout << "How many categories do you want to add?\n>";
					cin >> z;
					for(int j(0);j<z;++j){
						cout << "Enter category name:\n>";
						cin >> x;
						cout << "Enter category ID:\n>";
						cin >> y;
						for(int i(0);i<v.size();++i){ //Check to make sure no duplicate categories are created
							if(x==v[i].getCatName()){
								cout << "Category name already exists. Try a different name.\n";
								--j;
								break;
							}
							else if(y==v[i].getCatId()){
								cout << "Duplicate category ID. Try a different ID.\n";
								--j;
								break;
							}
						}
						c=Category(y,x); //create a new category with input values and add it to the category vector.
						v.push_back(c);
					}
					cout << "The categories are now as follows:\nCategory:\tID:\n";
					for(int i=0;i<v.size();++i)   //Output final categories
						cout << v[i].getCatName() << '\t' << v[i].getCatId() << '\n';
					break;
					   }
					   //End NJG 1C
				default: cout<<"Invalid Input"<<endl; break;
				}
			break;
			   }
		//end NJG 1S
		case 2:{
			cout<<"Adding a new customer"<<endl;
			//VCN update function PartB S2
			//cout << "Update queries \n";
			int cust_id, house_num, zip_code;	//necessary info
			string cust_name, street, line;
			Street_address addr;
			Customer c;
			cout<<"Enter a Customer ID: ";
			cin>>cust_id;
			cout<<"Enter the Customer Name: ";
			cin>>cust_name;
			cout<<"Enter the House Number: ";
			cin>>house_num;
			cout<<"Enter the Street Name: ";
			cin>>street;
			cout<<"Enter the Zip Code: ";
			cin>>zip_code;
			addr = Street_address(house_num, street, zip_code);//assign values to addr
			c = Customer(cust_id, cust_name, addr);		//assign values to customer				
			custs.push_back(c);	
			cout<<"Customer data updated"<<endl;
			break;
			}
		case 3:{
			cout<<"Choose an option:\n\t1) Add a new cart item to an existing cart\n\t";
			cout<<"2) Add a new cart given a Customer and Date\n\t3) Delete a cart item from a cart\n> ";
			int y;
			cin>>y;
			switch(y){
				case 1:{
					//Rendy PartB S3
					//cout << "Update queries \n";
					cout << "Insert your cart id : ";
					int cart_id;
					cin >> cart_id;
					int i = 0;
					//check cart_id validity
					while(i<carts.size() && cart_id!=carts[i].getCartId()){
						i++;
					}
					if(i>=carts.size())
						cout << "Cart doesn't exist.\n";
					else{		
						bool valid = false;		
						int prod_id;
						int prod_qty;
						
						cout << "Insert product id : ";		
						//check product id validity
						while(!valid){			
							cin >> prod_id;
							for(int j=0; j<prods.size(); j++){
								if(prod_id == prods[j].getProdId())
									valid = true;
							}
							if(!valid)
								cout << "Product id doesn't exist. Please enter a valid id : ";
							else
								cout << "Insert product id : ";		
						}
						
						cout << "Insert product quantity : ";		
						cin >> prod_qty;
						
						//look for suitable cart_id and product_id
						int j = 0;
						while(j<cart_items.size() && cart_items[j].getCartId() != cart_id && cart_items[j].getProdId()!=prod_id){
							j++;
						}
						//add quantity to existing product
						if(j<cart_items.size()){
							cart_items[j] = Cart_item(
								cart_items[j].getCartItemId(),
								cart_items[j].getCartId(),
								cart_items[j].getProdId(),
								cart_items[j].getProdQty()+prod_qty
							);
						} else { //add new cart item
							int cart_item_id = cart_items[cart_items.size()-1].getCartItemId()+1;
							Cart_item new_cart_item = Cart_item(
								cart_item_id,
								cart_id,
								prod_id,
								prod_qty
							);
							cart_items.push_back(new_cart_item);			
						}
					}
					break;}
				case 2: {
					//VCN PartC2 b)
					int cust_id,y,m,d;
					int cart_id=0;
					bool check=false;
					cout<<"Enter a Customer ID to add the cart to: "; 
					cin>>cust_id;
					for (int i=0; i<custs.size(); i++){
						if (cust_id==custs[i].getCustId()) check=true; //check if cust ID exists
					}
					if (!check){ cout<<"Customer doesn't exist"; return; } //return otherwise
					check=false;
					while(!check){
						cout<<"Enter the year: "; cin>>y;
						cout<<"Enter the month (numerical): "; cin>>m;
						cout<<"Enter the day of the month: "; cin>>d;
						if (!(m>0&&m<13)||!(d>0&&d<32))
							cout<<"Invalid date. Input a valid date.\n";
						else check=true; //true if date is valid
					}
					if(carts.size()!=0) cart_id=(carts.back()).getCartId();
					cart_id++;
					carts.push_back(Cart(cart_id, cust_id, Date(y, (Date::Month)m, d)));
					cout<<"Cart added successfully\nCart ID: "<<cart_id;
					break;}
				case 3: {
					//VCN PartC2 c)
					int cart_id, cart_item_id;
					bool check=false;
					vector<int>test;
					cout<<"Enter a cart ID to delete from: ";
					cin>> cart_id;
					
					for (int i=0; i<carts.size(); i++){
						if (cart_id==carts[i].getCartId()) check=true; //check if cart ID exists
					}
					if (!check){ cout<<"Cart ID doesn't exist"; return; } //return otherwise
					
					for(int i=0; i<cart_items.size();i++){
						if (cart_items[i].getCartId()==cart_id) 
							test.push_back(cart_items[i].getCartItemId());
					}
					if(test.size()==0) {cout<<"This cart is empty"; return;} //checks if cart is empty
					
					cout<<"Here are the Cart Item IDs for that cart:"<<endl;
					for(int i=0; i<test.size(); i++)
						cout<<test[i]<<endl;			//prints cart items for user to select from
					
					check=false;
					cout<<"Enter a cart item ID to delete: ";
					cin>>cart_item_id;
					for(int i=0; i<test.size(); i++)
						if(test[i]==cart_item_id) check=true;
					if(!check) {cout<<"Cart Item ID was not found"; return;}
					for(int i=0; i<cart_items.size();i++){
						if (cart_items[i].getCartItemId()==cart_item_id){
							cart_items.erase(cart_items.begin()+i); 
						}
					}
					cout<<"Cart item successfully deleted from the cart"<<endl;
					cout<<"Here are the updated Cart Item IDs for that cart:"<<endl;
					for(int i=0; i<cart_items.size();i++){
						if (cart_items[i].getCartId()==cart_id) 
							cout<<cart_items[i].getCartItemId()<<endl;
					}
								
					break;}
				default: cout<<"Invalid Input"<<endl; break;
}}
			break;
		default: cout<<"Invalid Input"<<endl; break;
}
}

void Menu::write_all()
{	
	cout << "Enter file name or file path to write category and product.\n> ";
	string catprd_file;
	cin >> catprd_file;
	ofstream fout (catprd_file.c_str());
	fout << cats.size() << "\n";
	for(int i=0; i<cats.size(); i++){
		fout << cats.at(i).write();
	}
	fout << "\n" << prods.size() << "\n";
	for(int i=0; i<prods.size(); i++){
		fout << prods.at(i).write();
	}	
		
	cout << "Enter file name or file path to write customer.\n> ";
	string cust_file;
	cin >> cust_file;
	ofstream fout1 (cust_file.c_str());	
	fout << custs.size() << "\n";
	for(int i=0; i<custs.size(); i++){
		fout1 << custs.at(i).write();
	}
	
	cout << "Enter file name or file path to write carts.\n> ";
	string carts_file;
	cin >> carts_file;
	ofstream fout2 (carts_file.c_str());
	fout2 << carts.size() << "\n";
	for(int i=0; i<carts.size(); i++){
		fout2 << carts.at(i).write();
	}
	fout2 << "\n" << cart_items.size() << "\n";
	for(int i=0; i<cart_items.size(); i++){
		fout2 << cart_items.at(i).write();
	}	
}

bool Menu::error_check(){
	bool error = false;

	for(int i=0; i<cart_items.size(); i++){
		bool prod_exist = false;
		int j = 0;
		while(j<prods.size() && !prod_exist){
			prod_exist = prods.at(j).getProdId() == cart_items.at(i).getProdId();
			if (!prod_exist) j++;
		}
		if(j>=prods.size()){
			cout << "Error no product exists : " << cart_items.at(i).display() << "\n";
			error = true;
		}
	}	
	for(int i=0; i<carts.size(); i++){
		bool cart_item_exist = false;
		int j = 0;
		while(j<cart_items.size() && !cart_item_exist){
			cart_item_exist = carts.at(i).getCartId() == cart_items.at(j).getCartId();
			j++;
		}
		if(j>=cart_items.size()){
			cout << "Error no item in cart : " << carts.at(i).display() << "\n";
			error = true;
		}
	}
	for(int i=0; i<prods.size(); i++){
		if(prods.at(i).getProdPrice()<0){
			cout << "Error price negative : " << prods.at(i).display() << "\n";
			error = true;
		}
	}

	//added
	return error;
}
